package com.siruier.boss.ui.adapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsBuildBitmapOption
import com.huawei.hms.ml.scan.HmsScan
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.dp
import com.siruier.boss.ui.utils.Logger
import com.youth.banner.adapter.BannerAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
 * 自定义布局，图片
 */
class BannerShareImageAdapter(
    private val appShareUrl: String,
    mDatas: List<String>
) : BannerAdapter<String, ImageHolder>(mDatas) {
    companion object {

        @JvmStatic
        fun generateQrcodeBitmap(context: Context, data: String, content: String): Bitmap? {
            try {
                val file = Glide.with(context)
                    .asFile()
                    .load(data)
                    .submit()
                    .get()
                val bgQrcode = BitmapFactory.decodeFile(file.path)
                val srcWidth = bgQrcode.width
                val srcHeight = bgQrcode.height
                val bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888)
                val canvas = Canvas(bitmap)
                canvas.drawBitmap(bgQrcode, 0f, 0f, null)
                val qrcodeWidth = 570
                val options = HmsBuildBitmapOption.Creator().setBitmapMargin(0).create()
                val qrBitmap = ScanUtil.buildBitmap(content, HmsScan.QRCODE_SCAN_TYPE, qrcodeWidth, qrcodeWidth, options)
                val qrcodeTop = srcHeight - 670 - qrcodeWidth
                canvas.drawBitmap(qrBitmap, 510f, qrcodeTop.toFloat(), null)
                return bitmap
            } catch (e: Exception) {
                Logger.e(e)
            }
            return null
        }
    }

    //更新数据
    fun updateData(data: List<String>) {
        mDatas.clear()
        mDatas.addAll(data)
        notifyDataSetChanged()
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): ImageHolder {
        val imageView = ImageView(parent.context)
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        val params: ViewGroup.LayoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.layoutParams = params
        return ImageHolder(imageView)
    }

    override fun onBindView(holder: ImageHolder, data: String, position: Int, size: Int) {
        (holder.imageView.context as? BaseActivity<*>)?.lifecycleScope?.launch {
            val bgQrcode = withContext(Dispatchers.IO) {
                generateQrcodeBitmap(holder.imageView.context, data, appShareUrl)
            }
            Glide.with(holder.imageView.context)
                .load(bgQrcode)
                .apply(
                    RequestOptions().transform(
                        RoundedCorners(10.dp)
                    )
                )
                .into(holder.imageView)
        }
    }
}

class ImageHolder(val imageView: ImageView) : RecyclerView.ViewHolder(imageView)